package com.example.front.main.user;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.front.MyApplication;
import com.example.front.R;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

public class SexModify extends AppCompatActivity {

    Bundle bundle = new Bundle();
    private MyApplication myApplication;
    private String url;
    private String tag;

    @Override
    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        setContentView(R.layout.sex_modify);

        tag = "0";
        myApplication = (MyApplication) getApplication();
        url = myApplication.getURL();
    }

    public void onRadioButtonClicked(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        switch(view.getId()) {
            case R.id.sexman:
                if (checked)
                    tag = "1";
                    bundle.putString("sex","1");
                    break;
            case R.id.sexwoman:
                if (checked)
                    tag = "1";
                    bundle.putString("sex","0");
                    break;
        }
    }

    public void sexModify(View view){
        if (tag.equals("0")){
            Toast.makeText(SexModify.this,"请选择性别",Toast.LENGTH_SHORT).show();
        }else {
            String sex = bundle.getString("sex");
            SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
            String account = sharedPreferences.getString("account", "");
            OkHttpUtils.post()
                    .url(url + "/user/msex")
                    .addParams("account", account)
                    .addParams("sex", sex)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            Toast.makeText(SexModify.this, "网络错误，修改失败", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onResponse(String response) {
                            if (response.equals("true")) {
                                SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putInt("sex", Integer.parseInt(sex));
                                editor.apply();
                                Toast.makeText(SexModify.this, "修改成功", Toast.LENGTH_SHORT).show();
                                finish();
                            } else {
                                Toast.makeText(SexModify.this, "系统错误，修改失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }
    }
}
